#!/bin/bash

# 检查并删除 build 目录
if [ -d "build" ]; then
    echo "Deleting the build directory..."
    rm -rf build
fi

# 创建新的 build 目录
echo "Creating a new build directory..."
mkdir build

# 进入 build 目录
cd build

# 执行 cmake 命令
echo "Running cmake..."
cmake ..

# 检查 cmake 是否成功
if [ $? -ne 0 ]; then
    echo "CMake failed. Exiting..."
    exit 1
fi

# 执行 make 命令
echo "Running make..."
make

# 检查 make 是否成功
if [ $? -ne 0 ]; then
    echo "Make failed. Exiting..."
    exit 1
fi

# 假设可执行文件名为 learnJsocCpp
EXECUTABLE_NAME="learnJsocCpp"
EXECUTABLE_PATH="./$EXECUTABLE_NAME"

# 检查可执行文件是否存在
if [ -f "$EXECUTABLE_PATH" ]; then
    echo "Running the executable..."
    $EXECUTABLE_PATH
else
    echo "Executable $EXECUTABLE_NAME not found. Please check your CMakeLists.txt."
fi
